<template>
    <div class="choose_address_container">
        <HeaderTop :goBack="true" :headTitle="'选择地址'"></HeaderTop>
        <section class="address_list" v-if="addressList.length">
            <Scroll>
                <div>
                    <ul class="deliverable_address">
                        <li v-for="(item,index) in deliverable" :key="index" 
                            @click.prevent.stop="chooseAddr(item, index)">
                            <svg class="choosed_address" :class="{default_address: defaultIndex == index}">
                                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#select"></use>
                            </svg>
                            <div>
                                <header>
                                    <span>{{item.name}}</span>
                                    <span>{{item.sex == 1? '先生' : '女士'}}</span>
                                    <span>{{item.phone}}</span>   
                                </header>
                                <div class="address_detail ellipsis">
                                    <span v-if="item.tag" :style="{backgroundColor: iconColor(item.tag)}">{{item.tag}}</span>
                                    <p>{{item.address_detail}}</p>   
                                </div>
                            </div>
                        </li>
                    </ul>
                    <section class="out_delivery" v-if="deliverdisable.length">
                        <header class="out_header">以下地址超出配送范围</header>
                        <ul class="deliverable_address">
                            <li v-for="(item,index) in deliverdisable" :key="index">
                                <svg class="choosed_address">
                                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#select"></use>
                                </svg>
                                <div>
                                    <header>
                                        <span>{{item.name}}</span>
                                        <span>{{item.sex == 1? '先生' : '女士'}}</span>
                                        <span>{{item.phone}}</span>   
                                    </header>
                                    <div class="address_detail ellipsis">
                                        <span v-if="item.tag" :style="{backgroundColor: '#ccc', color: '#fff'}">{{item.tag}}</span>
                                        <p>{{item.address_detail}}</p>   
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </section>
                </div>
            </Scroll>
        </section>
        <router-link to="/confirmOrder/chooseAddress/addAddress" class="add_icon_footer" >
            <img :src="add_img" height="24" width="24">
            <span>新增收货地址</span>
        </router-link>
        <div class="footer_place"></div>
        <transition name="route-slide">
            <router-view></router-view>
        </transition>
    </div>
</template>

<script>
import {mapState, mapActions} from 'vuex'
import {imgBasePath} from '@src/utils/config'
import Scroll from '@src/components/common/Scroll'
import {getAddressList} from '@api/getData';
export default {
    components:{
        Scroll
    },
    computed:{
        ...mapState(['user','newAddress']),
        //选择地址
        defaultIndex: function (){
            if (this.addressIndex) {
                return this.addressIndex;
            }else{
                return 0;
            }
        },
        
    },
    data(){
        return {
            imgBasePath,
            add_img:require('../../../assets/imgs/add_address.png').default,
            id:'', 
            sig:'',
            addressList: [], //地址列表
            deliverable: [], //有效地址列表
            deliverdisable: [], //无效地址列表
        }
    },
    created(){
        this.id = this.$route.query.id;
        this.sig = this.$route.query.sig;
        this.initData();
    },
    methods:{
        ...mapActions(['chooseAddress']),
        async initData(){
            this.addressList = [];
            this.deliverable = [];
            this.deliverdisable = [];
            if (this.user && this.user.user_id) {
                this.addressList = await getAddressList(this.user.user_id);
                //将当前所有地址访问有效无效两种
                this.addressList.forEach(item => {
                    if (item.is_deliverable) {
                        this.deliverable.push(item);
                    }else{
                        this.deliverdisable.push(item);
                    }
                })
            }
        },
        iconColor(name){
            switch(name){
                case '公司': return '#4cd964';
                case '学校': return '#3190e8';
                case '家': return '#EE6AA7';
                default : return '#CD853F';
            }
        },
        // 选择地址
        chooseAddr(address, index){
            this.chooseAddress({address, index});
            this.$router.go(-1);
        },
    },
    watch:{
        newAddress(){
            this.initData();
        }
    }
}
</script>

<style lang="scss" scoped>
@import '~@styl/mixin.scss';
.choose_address_container{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 220;
    background: #FFF;
    padding-top: 1.95rem;
    display: flex;
    flex-direction: column;
    .address_list{
        flex: 1;
        overflow-y: hidden;
        .deliverable_address{
            li{
                padding: 0.6rem;
                border-bottom: 0.012rem solid #f2f2f2;
                display: flex;
                align-items: center;
                .choosed_address{
                    @include wh(1rem,1rem);
                    fill: #fff;
                    margin-right: 0.5rem;
                    &.default_address{
                        fill: $green;
                    }
                }
                header{
                    span:nth-child(1){
                        @include sc(0.8rem,#333);
                        font-weight: 700;
                    }
                    span:nth-child(2),span:nth-child(3){
                        @include sc(0.7rem,#333);
                    }
                }
                .address_detail{
                    display: flex;
                    span{
                        @include sc(0.4rem,#fff);
                        padding: 0rem 0.2rem;
                        border-radius: 0.1rem;
                        display: flex;
                        align-items: center;
                        margin-right: 0.3rem;
                    }
                    p{
                        @include sc(0.6rem,#666);
                    }
                }
            }
        }
        .out_delivery{
            .out_header{
                
            }
        }
    }
    .add_icon_footer{
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 2.5rem;
        @include fj(center);
        align-items: center;
        span{
            margin-left: 0.2rem;
            @include sc(0.7rem,$blue)
        }
    }
    .footer_place{
        height: 2.5rem;
    }
}
.route-slide-enter,
.route-slide-leave-to
{
    transform: translateX(4rem);
    opacity: 0;
}
.route-slide-enter-active,
.route-slide-leave-active{
    transition: all 0.3s ease-in-out;
}
</style>